package com.gin.kotlin.no_gay_alliance.adapter.replyhistory

import android.content.Context
import android.content.DialogInterface
import android.view.View
import com.gin.kotlin.no_gay_alliance.R
import com.gin.kotlin.no_gay_alliance.activity.ReplyListActivity
import com.gin.kotlin.no_gay_alliance.app.App
import com.gin.kotlin.no_gay_alliance.argument.ReplyListArgument
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.AlertDialogUtils
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.database.entity.ReplyHistoryTopic
import com.gin.kotlin.no_gay_alliance.databinding.ItemReplyHistoryTopicBinding

class ReplyHistoryTopicAdapter(context: Context, handler: MyHandler) : AbstractBindingRecyclerViewAdapter<ItemReplyHistoryTopicBinding, ReplyHistoryTopic>(context, handler),
    View.OnClickListener {
    override fun onBindViewHolder(holder: ViewHolder<ItemReplyHistoryTopicBinding>, position: Int, b: ItemReplyHistoryTopicBinding, dataModel: ReplyHistoryTopic) {
        b.tvTitle.text = dataModel.title
        b.tvCount.text = "已记录: ${dataModel.count} 条"

        listOf(b.tvTitle, b.ivDelete).forEach {
            it.tag = dataModel
            it.setOnClickListener(this)
        }
    }

    override fun onClick(v: View?) {
        val replyHistoryTopic = v?.tag as? ReplyHistoryTopic ?: return
        val index = data.indexOf(replyHistoryTopic)
        when (v.id) {
            // 打开主题
            R.id.tv_title -> ReplyListActivity.startActivity(context, ReplyListArgument(ReplyListArgument.Type.LIST, topicId = replyHistoryTopic.topicId))
            // 弹窗确认移除
            R.id.iv_delete -> AlertDialogUtils.builder(
                context,
                "移除观察",
                replyHistoryTopic.title,
                listener = { _, which -> if (which == DialogInterface.BUTTON_POSITIVE) {
                    App.INSTANCE.database.replyHistoryTopicDao().del(replyHistoryTopic.topicId)
                    removeData(index)
                } }).show()
        }
    }
}